.TH std::ios_base::failure 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::ios_base::failure \- std::ios_base::failure

.SH Synopsis
   Defined in header <ios>
   class failure;

   The class std::ios_base::failure defines an exception object that is thrown on
   failure by the functions in the Input/Output library.

   std::ios_base::failure may be defined either as a member class of
   std::ios_base or as a synonym \fI(typedef)\fP for another class with         \fI(since C++17)\fP
   equivalent functionality.

   std-ios base-failure-2003-inheritance.svg
                                             \fI(until C++11)\fP
              Inheritance diagram
   std-ios base-failure-inheritance.svg
                                             \fI(since C++11)\fP
              Inheritance diagram

.SH Member functions

   constructor   constructs a new failure object with the given message
                 \fI(public member function)\fP
   operator=     replaces the failure object
                 \fI(public member function)\fP
   what          returns the explanatory string
                 \fI(public member function)\fP

std::ios_base::failure::failure

   explicit failure( const std::string& message );          \fI(until C++11)\fP
   explicit failure( const std::string& message,
                     const std::error_code& ec =            \fI(since C++11)\fP
   std::io_errc::stream );
   explicit failure( const char* message,           \fB(1)\fP
                     const std::error_code& ec =        \fB(2)\fP \fI(since C++11)\fP
   std::io_errc::stream );
   failure( const failure& other );                     \fB(3)\fP               \fI(until C++11)\fP
   failure( const failure& other ) noexcept;                              \fI(since C++11)\fP

   1,2) Constructs the exception object using message as explanation string which can
   later be retrieved using what().
   ec is used to identify the specific reason for the failure.
   \fI(since C++11)\fP
   3) Copy constructor. Initialize the contents with those of other.
   If *this and other both have dynamic type std::ios_base::failure then
   std::strcmp(what(), other.what()) == 0.
   \fI(since C++11)\fP

.SH Parameters

   message - explanatory string
   ec      - error code to identify the specific reason for the failure
   other   - another failure to copy

.SH Notes

   Because copying std::ios_base::failure is not permitted to throw exceptions, this
   message is typically stored internally as a separately-allocated reference-counted
   string. This is also why there is no constructor taking std::string&&: it would have
   to copy the content anyway.

std::ios_base::failure::operator=

   failure& operator=( const failure& other );           \fI(until C++11)\fP
   failure& operator=( const failure& other ) noexcept;  \fI(since C++11)\fP

   Assigns the contents with those of other.
   If *this and other both have dynamic type std::ios_base::failure then
   std::strcmp(what(), other.what()) == 0 after assignment.
   \fI(since C++11)\fP

.SH Parameters

   other - another exception object to assign with

.SH Return value

   *this

std::ios_base::failure::what

   virtual const char* what() const throw();   \fI(until C++11)\fP
   virtual const char* what() const noexcept;  \fI(since C++11)\fP

   Returns the explanatory string.

.SH Parameters

   \fI(none)\fP

.SH Return value

   Pointer to a null-terminated string with explanatory information. The string is
   suitable for conversion and display as a std::wstring. The pointer is guaranteed to
   be valid at least until the exception object from which it is obtained is destroyed,
   or until a non-const member function (e.g. copy assignment operator) on the
   exception object is called.

.SH Notes

   Implementations are allowed but not required to override what().

Inherited from std::system_error

.SH Member functions

   code      returns error code
             \fI(public member function of std::system_error)\fP
   what      returns an explanatory string
   \fB[virtual]\fP \fI(virtual public member function of std::system_error)\fP

Inherited from std::runtime_error

Inherited from std::exception

.SH Member functions

   destructor   destroys the exception object
   \fB[virtual]\fP    \fI(virtual public member function of std::exception)\fP
   what         returns an explanatory string
   \fB[virtual]\fP    \fI(virtual public member function of std::exception)\fP

.SH Notes

   Before the resolution of LWG issue 331, std::ios_base::failure declared a destructor
   without throw(), where std::exception::~exception() was declared with throw()^[1].
   This means the std::ios_base::failure::~failure() had a weaker exception
   specification. The resolution is to remove that declaration so that the non-throwing
   exception specification is kept.

   LWG issue 363 targets the same defect and its resolution is to add throw() to the
   declaraion of std::ios_base::failure::~failure(). That resolution was not applied
   due to the confict between the two resolutions.

    1. ↑ The non-throwing exception specification is now applied globally across the
       standard library, so the destructors of standard library classes are not
       declared with throw() or noexcept.

.SH Example


// Run this code

 #include <fstream>
 #include <iostream>

 int main()
 {
     std::ifstream f("doesn't exist");

     try
     {
         f.exceptions(f.failbit);
     }
     catch (const std::ios_base::failure& e)
     {
         std::cout << "Caught an ios_base::failure.\\n"
                   << "Explanatory string: " << e.what() << '\\n'
                   << "Error code: " << e.code() << '\\n';
     }
 }

.SH Possible output:

 Caught an ios_base::failure.
 Explanatory string: ios_base::clear: unspecified iostream_category error
 Error code: iostream:1

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

     DR    Applied to           Behavior as published               Correct behavior
                      the constructor overload \fB(1)\fP initialized
   LWG 48  C++98      the base class std::exception              corresponding
                      with msg, but the base class does not have description removed
                      a matching constructor
   LWG 331 C++98      std::ios_base::failure declared a          removed the destructor
                      destructor without throw()                 declaration

.SH See also

   io_errc the IO stream error codes
   \fI(C++11)\fP \fI(enum)\fP
